import { source_money_ratio_APP, source_getstoreroomListouth5, source_getstoreroomListinh5 } from '@/api/statistics_analysis'
import { getCurMonth, getCountDays } from '@/utils/vm-common-getTime.js'
const state = {
  // 统计分析的询源分析 start
  newSourceData: new Array(),
  currentMouth: getCurMonth(),
  sum: 0,
  // 统计分析的询源分析 end


  index: 0, // 统计分析出入库索引值
  // 统计分析的出库数据 start
  out_of_storageData : new Array(),
  out_of_storageSum:0,
  out_of_storagecurrentMouth:getCurMonth(),
  // 统计分析的出库数据 end

  // 统计分析的入库数据 start
  WarehousingData : new Array(),
  WarehousingSum:0,
  WarehousingMouth:getCurMonth()
  // 统计分析的入库数据 end
};

const getters = {};

const mutations = {

  // 出库、入库所有
  SET_INDEX (state, index) {
    state.index = index;
  },

  // 询源分析
  SET_NEWSOURCEDATA (state, data) {
    state.newSourceData = data;
  },
  SET_CURRENTMOUTH (state,currentMouth) {
    state.currentMouth = currentMouth;
  },
  SET_SUM (state, sum) {
    state.sum = sum;
  },


  /* 出库 start */
  // 出库数据
  SET_OUTOFSTORAGEDATA (state, data) {
    state.out_of_storageData = data;
  },
  // 出库总数量
  SET_OUTOFSTORAGESUM (state, sum) {
    state.out_of_storageSum = sum;
  },
  // 出库月份
  SET_OUTCURRENTMOTCH (state, currentMouth) {
    state.out_of_storagecurrentMouth = currentMouth;
  },
  /* 出库 end */

  /* 入库 start */
  // 入库数量
  SET_WAREHOUSINGDATA (state,data) {
    state.WarehousingData = data;
  },
  // 出库总数量
  SET_WAREHOUSINGSUM (state, sum) {
    state.WarehousingSum = sum;
  },
  // 出库月份
  SET_WAREHOUSINGMOUTH (state, currentMouth) {
    state.WarehousingMouth = currentMouth;
  }
  /* 入库 end */

};

const actions = {
  // 统计分析 -> 询源分析数据更改
  async getSourceMoneyRatio ({commit}, data = {startTime, endTime, month}) {
    let sourceResult = await source_money_ratio_APP(data.startTime, data.endTime).then(res => res.data.data);
    // 数据拼接
    sourceResult = sourceResult.map(item => {
      let obj = {};
      obj.value = item.value;
      obj.name = item.name;
      obj.percent = item.proportion;
      obj.contract_num = item.contract_num;
      switch (item.name) {
        case '招投标':
          obj.iconClass = 'bid';
          break;
        case '询价采购':
          obj.iconClass = 'money';
          break;
        case '竞争性谈判':
          obj.iconClass = 'competitive';
          break;
        case '单一来源':
          obj.iconClass = 'Single';
          break;
        case '竟比价':
          obj.iconClass = 'competitive-price';
          break;
        case '其他':
          obj.iconClass = 'other';
          break;
      }
      return obj;
    });
    sourceResult.sort((a, b) => {
      return b.value - a.value;
    });
    // 获取总和
    let sum = 0;
    sourceResult.forEach(item => {
      sum += new Number(item.value);
    });

    commit("SET_CURRENTMOUTH", data.month);
    commit("SET_SUM", sum);
    commit('SET_NEWSOURCEDATA', sourceResult);
  },
  // 统计分析 -> 出库数据更改
  async getOut_of_storageData ({commit}, {startTime, endTime, month, warehouse_id=''}) {
    let resultOutStorageData = await source_getstoreroomListouth5({startTime, endTime, warehouse_id}).then(res => res.data.data);
    let sum = resultOutStorageData.moneySum[0].Summoney;
    resultOutStorageData = resultOutStorageData.levs.map(item => {
      let obj = {};
      obj.value = item.money;
      obj.name = item.arrtype;
      obj.nums = item.nums;
      obj.percentNum = item.percentNum;
      switch (item.arrtype) {
        case '销售出库':
          obj.iconClass = 'cgrk';
          break;
        case '领料出库':
          obj.iconClass = 'scrk';
          break;
        case '盘亏出库':
          obj.iconClass = 'pyrk';
          break;
        case '调拨出库':
          obj.iconClass = 'dbck';
          break;
        case '其他出库':
          obj.iconClass = 'qtck';
          break;
        case '直运出库':
          obj.iconClass = 'zyck';
          break;
      }
      return obj;
    });
    resultOutStorageData.sort((a, b) => {
      return b.value - a.value;
    });
    // 触发存储出库月份
    commit("SET_OUTCURRENTMOTCH", month);
    // 触发存储出库价钱
    commit("SET_OUTOFSTORAGESUM", sum);
    // 触发存储出库数据
    commit("SET_OUTOFSTORAGEDATA",resultOutStorageData);
  },
  // 统计分析 -> 入库数据更改
  async getWarehousingData ({commit}, {startTime, endTime, month ,warehouse_id=''}) {
    let resultWarehousingData = await source_getstoreroomListinh5({startTime, endTime, warehouse_id}).then(res => res.data.data);
    let sum = resultWarehousingData.moneySum[0].Summoney;
    resultWarehousingData = resultWarehousingData.levs.map(item => {
      let obj = {};
      obj.value = item.money;
      obj.name = item.arrtype;
      obj.nums = item.nums;
      obj.percentNum = item.percentNum;
      switch (item.arrtype) {
        case '采购入库':
          obj.iconClass = 'cgrk';
          break;
        case '生产入库':
          obj.iconClass = 'scrk';
          break;
        case '盘盈入库':
          obj.iconClass = 'pyrk';
          break;
        case '调拨入库':
          obj.iconClass = 'dbck';
          break;
        case '其他入库':
          obj.iconClass = 'qtck';
          break;
        case '直运入库':
          obj.iconClass = 'zyck';
          break;
      }
      return obj;
    });
    resultWarehousingData.sort((a, b) => {
      return b.value - a.value;
    });
    // 触发存储入库月份
    commit("SET_WAREHOUSINGMOUTH", month);
    // 触发存储入库价钱
    commit("SET_WAREHOUSINGSUM", sum);
    // 触发存储入库数据
    commit("SET_WAREHOUSINGDATA",resultWarehousingData);
  }
};

export default {
  state,
  getters,
  actions,
  mutations
}
